/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.Image;
import com.jb.web.context.WebContext;

import java.io.File;

import com.jb.faces.component.UIComponent;


public class ImageRenderer extends LinkRenderer {
    public StringBuffer render(WebContext ctx, UIComponent item) {
        Image hItem = (Image) item;
        StringBuffer htmlBuffer = new StringBuffer("");

        if (!isVisible(ctx, hItem)) {
            return htmlBuffer;
        }

        String url = getURL(ctx, hItem);

        boolean isLink = !isEmptyString(url) && (ctx.isRunTime() || !hItem.isDesignable()) && !hItem.isDisabled();

        if (isLink) {
            htmlBuffer.append("<a ").append(addTagProperty("href", url, null))
                      .append(addTagProperty("class", "imglink", null))
                      .append(addTagProperty("id", hItem.getClientId() + "_ref", null)).append(">");
        }

        String desc = evaluate(ctx, hItem, hItem.getTitle());
        String src = hItem.getLocation();

        if (hItem.isDisabled() && (hItem.getDisableImage() != null)) {
            src = hItem.getDisableImage();
        }

        src = evaluate(ctx, hItem, src);

        if (!src.toLowerCase().startsWith("http://")) {
            File file = new File(ctx.getWebTop() + src);

            if ((!file.exists() || file.isDirectory()) && !isEmptyString(hItem.getDefaultValue())) {
                src = "" + evaluate(ctx, hItem, hItem.getDefaultValue());

                if (src.toLowerCase().endsWith("jpg")) {
                    file = new File(ctx.getWebTop() + src);
                }
            }

            boolean d = !file.exists() || file.isDirectory();

            if (d) {
                src = getIcon(ctx, src);
            }

            if (hItem.getChangeCount() != 0) {
                if (src.indexOf("?") != -1) {
                    src = src + "&_ac=" + hItem.getChangeCount();
                } else {
                    src = src + "?_ac=" + hItem.getChangeCount();
                }
            }
        }

        if (!isEmptyString(src) || !ctx.isRunTime()) {
            
            if (!isEmptyString(hItem.getHoverImage()) && !hItem.isDisabled()) {
                String hover = evaluate(ctx, hItem, hItem.getHoverImage());
                hItem.setOnMouseOver("this.src='" + hover + "'");
                hItem.setOnMouseOut("this.src='" + src + "'");
            }else {
                hItem.setOnMouseOver(null);
                hItem.setOnMouseOut(null);        
            }

            htmlBuffer.append("<img ").append(addTagProperty("src", src, null))
                      .append(addTagProperty("style", hItem.getStyle(ctx), null))
                      .append(addTagProperty("class", hItem.getStyleClass(), null))
                      .append(addTagProperty("title", desc, null)).append(addTagProperty("alt", desc, null))
                      .append(getEditScript(ctx, hItem)).append(" />");
        }

        if (isLink) {
            htmlBuffer.append("</a>");
        }

        return htmlBuffer;
    }

    protected String getURL(WebContext ctx, Image hItem) {
        String url =evaluate(ctx, hItem, hItem.getDestination());
        url = ctx.formURL(url, hItem);

        return url;
    }

    private String getIcon(WebContext ctx, String urlStr) {
        String url = urlStr.toLowerCase();
        String ext = url.substring(url.lastIndexOf(".") + 1);

        if ((ext != null) && (ext.length() > 0)) {
            url = "resources/images/ext/" + ext + ".gif";
        } else {
            url = "resources/images/ext/file.gif";
        }

        return url;
    }
}
